<?php
//分页类,此处仅处理分页的各种计算,不处理分页的显示方式
class TbfPage {
    public $ItemPerPage = 10;
    public $CurrentPage = 1;
    public $TotalItem;
    public $ShowPageNum = 10;
    public $urlCallback = null; //生成url的回调
    //默认添加url的方法
    function setDefaultUrlCallback($url){
        $this->urlCallback = function($page)use($url){
            return TbfHttp::addParamToUrl($url,'page',$page);
        };
    }
    function Init(){
        $this->CurrentPage = intval($this->CurrentPage);
        $this->ItemPerPage = intval($this->ItemPerPage);
        $this->TotalItem = intval($this->TotalItem);
        $this->ShowPageNum = intval($this->ShowPageNum);
        if ($this->CurrentPage<1){
            $this->CurrentPage = 1;
        }
        if ($this->CurrentPage>$this->GetTotalPage()){
            $this->CurrentPage = $this->GetTotalPage();
        }
        if ($this->ShowPageNum <=0){
            $this->ShowPageNum = 10;
        }
        if (empty($this->urlCallback)){
            TbfError::unExpect('[TbfPage:Init] empty($this->urlCallback)');
        }
    }

    //一共有多少页,最大页面(包含)
    function GetTotalPage(){
        $ret = (int)ceil(($this->TotalItem)/($this->ItemPerPage));
        if ($ret<=0){
            $ret = 1; //至少有1页
        }
        return $ret;
    }

    //是否有向前的按钮
    function IsBeforePageActive(){
        return $this->CurrentPage-1>=1;
    }
    //是否有向后的按钮
    function IsAfterPageActive(){
        return $this->CurrentPage+1<=$this->GetTotalPage();
    }
    //mysql数据库的偏移量
    function GetMysqlOffset(){
        return ($this->CurrentPage-1)*$this->ItemPerPage;
    }
    //前一页的url
    function GetBeforePageUrl(){
        $pageNum = $this->CurrentPage-1;
        if ($pageNum<1){
            $pageNum = 1;
        }
        $cb = $this->urlCallback;
        return $cb($pageNum);
    }
    //后台一页的url
    function GetAfterPageUrl(){
        $pageNum = $this->CurrentPage+1;
        if ($pageNum>$this->GetTotalPage()){
            $pageNum = $this->GetTotalPage();
        }
        $cb = $this->urlCallback;
        return $cb($pageNum);
    }

    //中间显示的分页的数据
    function GetShowPageArray(){
        //页面比显示数量还少,快速通道
        if ($this->GetTotalPage()<$this->ShowPageNum){
            return $this->getShowPageArrayFromNum(1,$this->GetTotalPage());
        }
        $start = $this->CurrentPage - floor($this->ShowPageNum/2);
        $end = $this->CurrentPage + floor($this->ShowPageNum/2);
        if ($start<1){
            $end += 1-$start;
            $start = 1;
        }
        if ($end>$this->GetTotalPage()){
            $start -= $end - $this->GetTotalPage();
            $end = $this->GetTotalPage();
        }
        return $this->getShowPageArrayFromNum($start,$end);
    }
    private function getShowPageArrayFromNum($start,$end){
        $output = array();
        $urlcb = $this->urlCallback;
        for ($i=$start;$i<=$end;$i++){
            $url = $urlcb($i);
            $output[] = array(
                'IsCurrent'=>$i==$this->CurrentPage,
                'PageNum'=>$i,
                'Url'=>$url,
            );
        }
        return $output;
    }

}
